//
//  JianzhiController.m
//  Ten
//
//  Created by 韦显光 on 2018/8/26.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "JianzhiController.h"
#import "MsgCell.h"
#import "PickerToolView.h"
#import "UIButton+WebCache.h"
#import "ShopHome2Controller.h"
#import "YMCitySelect.h"
#import "TuijianController.h"
#import "MoneyChoses.h"

@interface JianzhiController ()<YMCitySelectDelegate>
{
    int page;
    NSMutableArray*mary;
    NSString*company_id;
    NSString*cate_id;
    MoneyChoses*MoneyChosesView;
    NSString*salary_start;
    NSString*salary_end;
    
}
@property (weak, nonatomic) IBOutlet UITableView *tbv;
@property (weak, nonatomic) IBOutlet UIButton *btn1o;
@property (weak, nonatomic) IBOutlet UIButton *btn2o;
@property (weak, nonatomic) IBOutlet UIButton *btn3o;
@property (weak, nonatomic) IBOutlet UIButton *btn4o;
@property (weak, nonatomic) IBOutlet UILabel *lb1;
@property (weak, nonatomic) IBOutlet UIView *hidenView;

@end

@implementation JianzhiController

//tag==  0 地区  1职位类型 2薪资  3工资排序
- (IBAction)changgeBtna:(UIButton *)sender {
    _btn1o.selected=NO;
    _btn2o.selected=NO;
    _btn3o.selected=NO;
    _btn4o.selected=NO;
     sender.selected=YES;
    [mary removeAllObjects];
    
    if (sender.tag==0) {
         [self presentViewController:[[YMCitySelect alloc] initWithDelegate:self] animated:YES completion:nil];
        
    }else if (sender.tag==1)
    {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"NewHome" bundle:nil];
        ShopHome2Controller*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"ShopHome2"];
        secondViewController.completion = ^(NSDictionary *sriid) {
            [sender setTitle:WXGStr(sriid[@"name"]) forState:UIControlStateNormal];
            cate_id=WXGStr(sriid[@"cate_id"]);
             [self loaddata];
        };
        [self.navigationController pushViewController:secondViewController animated:YES];
        

    }else if (sender.tag==3)
    {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"NewHome" bundle:nil];
        TuijianController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Tuijian"];
        secondViewController.type=@"choses";
        secondViewController.completion = ^(NSDictionary *passdic) {
            [sender setTitle:passdic[@"name"] forState:UIControlStateNormal];
            company_id=WXGStr(passdic[@"company_id"]);
             [self loaddata];
        };
        [self.navigationController pushViewController:secondViewController animated:YES];

    }
    
    else
    {
        MoneyChosesView.hidden=NO;
        
    }

}
-(void)ym_ymCitySelectCityName:(NSString *)cityName{
    
    [_btn1o setTitle:cityName forState:UIControlStateNormal];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    cate_id=WXGStr(_passdic[@"cate_id"]);
    [_btn2o setTitle:WXGStr(_passdic[@"name"]) forState:UIControlStateNormal];
    _btn2o.selected=YES;
    _tbv.separatorColor=[UIColor clearColor];
//    _lb1.text=_type;
    _tbv.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        page=0;
        mary=[NSMutableArray array];
        [self loaddata];
    }];
    
    [_tbv.mj_header beginRefreshing];
    [_btn4o titleLabel].numberOfLines=0;
    [_btn1o titleLabel].numberOfLines=0;
    [_btn2o titleLabel].numberOfLines=0;
    [_btn3o titleLabel].numberOfLines=0;
    MoneyChosesView=[MoneyChoses instanceCenterViewWithType];
    MoneyChosesView.frame=CGRectMake(0, 0, DEVW, DEVH);
    [MoneyChosesView initMoney];
    MoneyChosesView.hidden=YES;
    __weak typeof(self) weakSelf = self;
    MoneyChosesView.completion = ^(NSString * _Nonnull money) {
        [weakSelf.btn3o setTitle:money forState:UIControlStateNormal];
        salary_start=[[money componentsSeparatedByString:@"-"]firstObject];
        salary_end=[[money componentsSeparatedByString:@"-"]lastObject];
         [weakSelf loaddata];
    };
    [self.view addSubview:MoneyChosesView];
}

-(void)loaddata
{
    NSMutableDictionary*mdic=[NSMutableDictionary dictionaryWithDictionary:@{@"page":[NSString stringWithFormat:@"%d",page]}];
    [mdic setObject:company_id forKey:@"company_id"];
    [mdic setObject:cate_id forKey:@"cate_ids"];
    [mdic setObject:salary_start forKey:@"salary_start"];
    [mdic setObject:salary_end forKey:@"salary_end"];
    
    [FuncPublic getData:self parameters:mdic RequestUrl:XjobsList success:^(NSDictionary *acountInfo) {
        [_tbv.mj_header endRefreshing];
        [_tbv.mj_footer endRefreshing];
        NSArray*tempary=acountInfo[@"job"];

        if (tempary.count) {
            [mary addObjectsFromArray:tempary];
            _tbv.mj_footer=[MJRefreshAutoFooter footerWithRefreshingBlock:^{
                page++;
                [self loaddata];
            }];
        }else
        {
            _tbv.mj_footer=nil;
        }
        
        _hidenView.hidden=mary.count>0?YES:NO;
        
        [_tbv reloadData];
        
    }];

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 返回分组数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return mary.count;
}

#pragma mark 返回每组行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
    static NSString *ID = @"MsgCell";
    NSDictionary*dic=mary[indexPath.section];
    MsgCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.lb1.text=WXGStr(dic[@"title"]);
  
    cell.lb2.text=[NSString stringWithFormat:@"￥%@/%@",dic[@"salary"],[WXGStr(dic[@"salary_type"])isEqualToString:@"2"]?@"月":@"日"];
     cell.lb3.text=WXGStr(dic[@"address"]);
     cell.lb4.text=WXGStr(dic[@"add_time"]);
    [cell.imgbtno sd_setImageWithURL:[dic[@"company"][@"logo"]formatUrl] forState:UIControlStateNormal];
     [[cell.imgbtno imageView] setContentMode:UIViewContentModeScaleAspectFit];
    cell.lb5.text=WXGStr(dic[@"company"][@"name"]);
    return cell;
}
#pragma mark 设置分组头高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return section==0?0:10;
}


#pragma mark 设置尾部说明内容高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

#pragma mark 设置每行高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 165;
}
#pragma mark 点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{
    [MyUtils collviewSelidex:@"职位详情" toVc:self andShopInfo:mary[indexPath.section]];
}

@end
